using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using SkinningSample.ViewModel;

namespace SkinningSample
{
    /// <summary>
    /// The main application window. Manages swapping between different pages.
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// The root view-model object.
        /// </summary>
        private ViewModelManager vmRoot = null;

        /// <summary>
        /// Constructor.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Sets up the data context for the main window.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            this.vmRoot = App.TypedApp.ViewModel;
            this.vmRoot.Navigation.PageChanged += new EventHandler<PageChangedArgs>(Navigation_PageChanged);

            // Show the starting page.
            SetActiveView(this.vmRoot.Navigation.CurrentPage);
        }

        /// <summary>
        /// Event handler for the current page being changed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Navigation_PageChanged(object sender, PageChangedArgs e)
        {
            SetActiveView(this.vmRoot.Navigation.CurrentPage);
        }

        /// <summary>
        /// Swaps the currently displayed view to display the requested page instead.
        /// </summary>
        /// <param name="newPageViewModel">The view-model object of the new page to show.</param>
        private void SetActiveView(IPage newPageViewModel)
        {
            //
            // Perform a lookup on the view-model type to find the associated view.
            //
            Type pageType = newPageViewModel.GetType();
            UserControl newView = null;
            if (typeof(Page1ViewModel) == pageType)
            {
                newView = new View.Page1();
            }
            else if (typeof(Page2ViewModel) == pageType)
            {
                newView = new View.Page2();
            }
            else
            {
                Debug.Assert(false, "Unknown page type!");
            }

            //
            // Now assign the data context of the new view and display it.
            //
            if (null != newView)
            {
                newView.DataContext = newPageViewModel;
            }
            this.ContentTarget.Content = newView;
        }
    }
}
